package com.skt.context;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.util.Map;

/**
 * 请求上下文构建类
 * 用于封装一次请求中的所有上下文信息，便于在服务层传递和使用
 */
@Data
@Builder
@Schema(description = "请求上下文信息")
public class RequestContext {

    @Schema(description = "用户身份信息")
    private UserIdentity userIdentity;

    @Schema(description = "客户端信息")
    private ClientInfo clientInfo;

    @Schema(description = "业务上下文信息")
    private BusinessContext businessContext;

    @Schema(description = "追踪信息")
    private TraceContext traceContext;

    @Data
    @Builder
    @Schema(description = "用户身份信息")
    public static class UserIdentity {
        @Schema(description = "用户ID", example = "12345")
        private Long userId;

        @Schema(description = "租户ID", example = "skt_tenant_001")
        private String tenantId;

        @Schema(description = "用户角色", example = "USER")
        private String userRole;

        @Schema(description = "是否为匿名用户", example = "false")
        private Boolean isAnonymous;

        @Schema(description = "会话ID", example = "session_abc123")
        private String sessionId;

        @Schema(description = "设备ID", example = "device_xyz789")
        private String deviceId;
    }

    @Data
    @Builder
    @Schema(description = "客户端信息")
    public static class ClientInfo {
        @Schema(description = "客户端类型: APP/WEB/MINI_PROGRAM", example = "APP")
        private String clientType;

        @Schema(description = "应用版本", example = "1.2.3")
        private String appVersion;

        @Schema(description = "设备型号", example = "iPhone14,2")
        private String deviceModel;

        @Schema(description = "操作系统版本", example = "iOS 16.5")
        private String osVersion;

        @Schema(description = "网络类型: WIFI/4G/5G", example = "WIFI")
        private String networkType;

        @Schema(description = "客户端IP地址", example = "192.168.1.100")
        private String ipAddress;
    }

    @Data
    @Builder
    @Schema(description = "业务上下文信息")
    public static class BusinessContext {
        @Schema(description = "业务渠道: APP/WEB/API", example = "APP")
        private String channel;

        @Schema(description = "业务线标识", example = "costume_rental")
        private String businessLine;

        @Schema(description = "来源页面", example = "home_page")
        private String source;

        @Schema(description = "扩展业务参数")
        private Map<String, Object> extraParams;
    }

    @Data
    @Builder
    @Schema(description = "追踪信息")
    public static class TraceContext {
        @Schema(description = "全链路追踪ID", example = "trace_abc123xyz456")
        private String traceId;

        @Schema(description = "当前SpanID", example = "span_789def")
        private String spanId;

        @Schema(description = "请求时间戳", example = "1672531200000")
        private Long requestTime;

        @Schema(description = "请求路径", example = "/cart")
        private String requestPath;
    }
}